package Model;


import java.lang.reflect.Array;
import java.util.*;

public class DeleteSubTaskCommand implements Command
{
   //attributes
   private ComplexTask ParentTask;
   private ArrayList<Task> TasksPointer;
   private ArrayList<Task> SubTasksPointer;
   private Task DeletedSubTask;
   private boolean undone;

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public DeleteSubTaskCommand(ArrayList<Task> PointerToTasks)
	{
		TasksPointer=PointerToTasks;
        undone=false;
	}

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void execute(String taskName)
    {
        if(ParentTask == null)
		{
			for(int i = 0; i < TasksPointer.size(); ++i)
			{
                 if(TasksPointer.get(i).getType().equals("Complex"))
                 {
                    ArrayList<Task> temp = TasksPointer.get(i).getSubtasks();

                    for(int k = 0; k< temp.size();++k)
                    {
                        if(temp.get(k).getName().equals(taskName))
                        {
                            ParentTask = (ComplexTask)TasksPointer.get(i);
                            SubTasksPointer = temp;
                            DeletedSubTask = SubTasksPointer.get(k);
                            SubTasksPointer.remove(k);
                            break;
                        }
                    }//end of inner-for
                 }
			}//end of outer-for
		}//end of outer-if
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void undo()
    {
        if(ParentTask != null && undone==false)
		{
            ParentTask.addTodoItem(DeletedSubTask);
            undone=true;
        }
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void redo()
    {
        if(ParentTask != null && undone==true)
		{
            ParentTask.removeTask(DeletedSubTask);
            undone=false;
        }
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

}
